package com.gyms.view.bottomview;

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.classic.okhttp.AppChangeUtils;
import com.gyms.R;
import com.gyms.activity.OrderDetailActivity;
import com.gyms.manager.StatisticsManager;
import com.zhy.autolayout.AutoLinearLayout;

import utils.DecimalUtils;

/**
 * 支付方式选择框
 */
public class PaymentDialog extends BaseBottomDialog implements View.OnClickListener {
    AutoLinearLayout llayoutZhi;
    AutoLinearLayout llayoutWei;
    private ImageView closeBtn;
    private TextView mTvPrice;
    private Button mSure;
    private CheckBox mZhi, mWei;
    private int payWay = 0;//支付方式0支付宝，1微信;
    private String mAllPrice;//总价格
    private OnPayListener payListener;
    private boolean isZFBPay;//默认是支付宝
    private Context mContext;

    private TextView mTvGoRevertDetail;
    private OrderDetailActivity mOrderDetailActivity;
    private ImageView mIvRevertIcon;

    private LinearLayout mLlRevertRule;

    public PaymentDialog(Context context, int animationStyles, String prise) {
        super(context, R.style.BottomViewTheme_Defalut);
        mAllPrice = prise;
        this.mContext=context;
        mOrderDetailActivity= (OrderDetailActivity) context;
        animationStyle = animationStyles;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.dialog_payment;
    }

    /**
     * 设置支付方式监听
     *
     * @param payListener
     */
    public void setPayListener(OnPayListener payListener) {
        this.payListener = payListener;
    }

    @Override
    protected void initView() {
        mLlRevertRule= (LinearLayout) findViewById(R.id.ll_revert_rule);
        llayoutZhi = (AutoLinearLayout) findViewById(R.id.llayout_zhi);
        llayoutWei = (AutoLinearLayout) findViewById(R.id.llayout_wei);
        mZhi = (CheckBox) findViewById(R.id.cb_zhi);
        mWei = (CheckBox) findViewById(R.id.cb_wei);
        mIvRevertIcon= (ImageView) findViewById(R.id.iv_revert_icon);
        mTvGoRevertDetail= (TextView) findViewById(R.id.tv_go_revert_detail);
        mOrderDetailActivity.doShowRevertTime(mTvGoRevertDetail, mIvRevertIcon,mOrderDetailActivity.isProduct);
        mZhi.setChecked(isZFBPay);
        mWei.setChecked(!isZFBPay);
        mZhi.setOnClickListener(zhi);
        mWei.setOnClickListener(wei);
        mLlRevertRule.setOnClickListener(this);
        llayoutZhi.setOnClickListener(zhi);
        llayoutWei.setOnClickListener(wei);
        if(AppChangeUtils.isGymApp){//如果是场馆通 隐藏微信支付
            llayoutWei.setVisibility(View.VISIBLE);
        }else {
            llayoutWei.setVisibility(View.GONE);
        }

        closeBtn = (ImageView) findViewById(R.id.tv_close);
        mTvPrice = (TextView) findViewById(R.id.tv_total_price);
        mSure = (Button) findViewById(R.id.btn_confirm);
        mTvPrice.setText(new SpannableStringBuilder("实际支付：").append(DecimalUtils.setPriceStyle(mContext,Double.parseDouble(mAllPrice),R.dimen.price_text_size_21,R.dimen.price_text_size_18,true)));
//        setTextView(mPriae);
        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        mSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (payListener != null) {
                    if (mZhi.isChecked() == true) {
                        payWay = 0;
                    } else {
                        payWay = 1;
                    }
                    //订单支付按钮点击次数
                    StatisticsManager.onEvent(mContext, StatisticsManager.OrderDetail_Pay);
                    payListener.toPay(v, payWay);
                    dismiss();
                }
            }
        });
    }

    View.OnClickListener zhi = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mZhi.setChecked(true);
            mWei.setChecked(false);
        }
    };
    View.OnClickListener wei = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mZhi.setChecked(false);
            mWei.setChecked(true);
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_revert_rule:
                /*Bundle mBundle = new Bundle();
                mBundle.putBoolean(Constants.GO_RULE_DETAIL,true);
                mBundle.putString(Constants.WEBVIEW_TITLE,"退款详情");
                mBundle.putString(Constants.WEBVIEW_URL, Cons.URL_REVERT_RULE);
                mBundle.putSerializable(Constants.WEBVIEW_CONFIGS_LIST,((OrderDetailActivity)mContext).mRefundConfigs);
                ((BaseActivity)mContext).startActivity(WebViewActivity.class,mBundle,false);*/
                mOrderDetailActivity.goRevertDetail();
                break;
        }
    }

//    private void setTextView(String prise) {
//        prise += "元";
//        int end = prise.length();
//        Spannable textSpan = new SpannableStringBuilder(prise);
//        textSpan.setSpan(new AbsoluteSizeSpan(60), 0, end - 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//        textSpan.setSpan(new AbsoluteSizeSpan(40), end - 1, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//        mPrise.setText(textSpan);
//    }

    public interface OnPayListener {
        /**
         * 确认支付
         *
         * @param v
         * @param payWay 支付方式
         */
        void toPay(View v, int payWay);
    }
    /**
     * 设置支付方式的选项
     */
    public void setPayWay(boolean isZfBPay){
        isZFBPay=isZfBPay;
    }
}
